package com.putable.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * Every client connected to the server, gets a mirrored copy in the server called Client Proxy.
 * @author Event Horizon
 *
 */
public class CGWClientProxy {
        /** Reference to client socket. */
        private Socket clientSocket;

        /** Data output stream. */
        @SuppressWarnings("unused")
        private ObjectOutputStream clientOS = null;

        /** Raw input stream from server. */
        @SuppressWarnings("unused")
        private ObjectInputStream clientIS;

        /** An instance of this client thread. */
        private Thread clientThread;

        /** The user name of the client connected. Default is Guest */
        private String userName = "Guest";

        /** Checks to see if the client is logged in. */
        private boolean isLoggedin;

        /** Checks to see if the client is connected to the server. */
        private boolean isConnected;
        

        /**
         * Instantiates a new CGW Client Proxy.
         * 
         * @param in
         *            the socket from the client.
         * @param cgwServer
         *            the CGW server.
         */
        public CGWClientProxy(Socket in, CGWServer cgwServer) {
                clientSocket = in;
                try {
                        clientIS = new ObjectInputStream(clientSocket.getInputStream());
                        clientOS = new ObjectOutputStream(clientSocket.getOutputStream());
                } catch (IOException e) {
                        System.err.println("Unable to connect" + e);
                }

                this.isConnected = true;
                this.isLoggedin = false;

                clientThread = new Thread();
                clientThread.start();
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.lang.Runnable#run()
         */
        public void run() {
                while (!Thread.currentThread().isInterrupted() && isConnected) {
                        if (!clientSocket.isClosed()) {
                               // TODO Client Commands from here Parsing is done here
                               // Well not really here but goes into the queue.
                        }

                }

        }

 

        /**
         * Gets the username of the client.
         * 
         * @return the username of the client.
         */
        public String getName() {
                return this.userName;
        }
        
        public void setUserName(String name){
                this.userName = name;
        }

        /**
         * Sets the logged in status.
         * 
         * @param log
         *            in status of the client.
         */
        public void setLoggedIn(boolean status) {
                this.isLoggedin = status;
        }

        /**
         * Checks if, the client is logged in.
         * 
         * @return true, if the client is logged in.
         */
        public boolean isLoggedIn() {
                return this.isLoggedin;
        }

        /**
         * Checks if the client is connected.
         * 
         * @return true, if it is connected.
         */
        public boolean isConnected() {
                return this.isConnected;
        }

        /**
         * Kills the client Thread, safely.
         */
        public void killThread() {
                try {
                        this.clientSocket.close();
                } catch (IOException e1) {
                        System.err.println("CGWorldClientProxy("
                                        + this.userName + "): Could not close socket.");
                        e1.printStackTrace();
                }
                this.clientThread.interrupt();

                try {
                        this.clientThread.join();
                } catch (InterruptedException e) {
                        System.err.println("Thread failed to reach barrier");
                        e.printStackTrace();
                }

                this.clientThread = null;
        }

}
